package com.yangyang.androidconnect.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.FileOutputStream;

public class ThumbnailGenerator {

    public static void generateThumbnail(String inputImagePath, String outputThumbnailPath) {
        try {
            // 原始图片路径
            //String imagePath = "/path/to/your/image.jpg";
            BitmapFactory.Options options = new BitmapFactory.Options();
            // 设置 inJustDecodeBounds 为 true，只获取图片的尺寸信息，不真正加载图片到内存
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(inputImagePath, options);
            int imageWidth = options.outWidth;
            int imageHeight = options.outHeight;
            int thumbnailSize = 100; // 缩略图的大小，可以根据需求调整
            // 计算缩放比例
            int scale = Math.min(imageWidth / thumbnailSize, imageHeight / thumbnailSize);
            options.inJustDecodeBounds = false;
            options.inSampleSize = scale;
            Bitmap thumbnail = BitmapFactory.decodeFile(inputImagePath, options);
            // thumbnail 即为生成的缩略图，可以进行进一步的处理或保存

            FileOutputStream fos = new FileOutputStream(outputThumbnailPath);
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}